<?php
// Copyright (c) 2009, wiztv-modules project
// All rights reserved.

// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
// conditions are met:
// Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided with the distribution.
// Neither the name of the wiztv-modules project nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
// BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

header('Content-type: application/xml');
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");

// XML Entity Mandatory Escape Characters
function xmlentities( $string )
    {
    return str_replace( array ( '&', '"', "'", '<', '>' ), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' , '&gt;' ), $string );
    }

// user_id - the user's feed we are browsing
// album_id - the album we are browsing
// album_index - the album we are currently browsing
// photo_index - the photo we are currently viewing
// alpha - amount of transparency for picture viewer
$user_id = @$_GET["user_id"];
$album_id = @$_GET["album_id"];
$album_index = (int) @$_GET["album_index"];
$photo_index = (int) @$_GET["photo_index"];
$alpha = @$_REQUEST['alpha'];
if(empty($alpha)) $alpha = "0xFE000000";

$IMAGE_SIZE = 912; // 1280
$SMALLER_IMAGE_SIZE = 800; // For when a landscape image is too tall
$SCREEN_WIDTH = 1280;
$SCREEN_HEIGHT = 720;

$request_start = $photo_index + 1;

// build feed URL
$feedURL = "http://picasaweb.google.com/data/feed/api/user/$user_id/albumid/$album_id?start-index=$request_start&max-results=1&imgmax=$IMAGE_SIZE";

// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);

$left_event = "";
$right_event = "";
foreach ( $sxml->link as $link )
    {
    if ( $link['rel'] == "previous" )
        {
        $left_event = '<event name="OnLeftKey" command="NAVIGATE_PAGE_COMMAND" value="'.
                      xmlentities("photo.php?user_id=$user_id&album_id=$album_id&album_index=$album_index&photo_index=".($photo_index-1)).'"/>';
        }
    else if ( $link['rel'] == "next" )
        {
        $right_event = '<event name="OnRightKey" command="NAVIGATE_PAGE_COMMAND" value="'.
                       xmlentities("photo.php?user_id=$user_id&album_id=$album_id&album_index=$album_index&photo_index=".($photo_index+1)).'"/>';
        }
    }

$title = xmlentities($sxml->entry->title); // filename - or caption if available
$media = $sxml->entry->children('http://search.yahoo.com/mrss/');
$content = $media->group->content;
$image_url = $content->attributes()->{'url'};
$image_height = $content->attributes()->{'height'};
$image_width = $content->attributes()->{'width'};

if ( $image_height > $SCREEN_HEIGHT )
    {
    if ( $image_height == $IMAGE_SIZE )
        {
        // image is portrait (or square) - use a smaller image size (one that will fit in the screen height)
        
        $image_width = round( $SCREEN_HEIGHT / $image_height * $image_width );
        $image_height = $SCREEN_HEIGHT;
        
        // modify the image URL to request the one $SCREEN_HEIGHT high
        $image_url = str_replace( "/s$IMAGE_SIZE/", "/s$SCREEN_HEIGHT/", $image_url );
        }
    else
        {
        // image is landscape, but too tall
        // (height is somewhere between $SCREEN_HEIGHT and $IMAGE_SIZE)
        
        if ( $image_width / $SMALLER_IMAGE_SIZE * $image_height > $SCREEN_HEIGHT )
            {
            // take the image size down one step
            $image_height = round( $SMALLER_IMAGE_SIZE / $image_width * $image_height );
            $image_width = $SMALLER_IMAGE_SIZE;
            
            // modify the image URL to request the one $SMALLER_IMAGE_SIZE high
            $image_url = str_replace( "/s$IMAGE_SIZE/", "/s$SMALLER_IMAGE_SIZE/", $image_url );
            }
            
        if ( $image_height > $SCREEN_HEIGHT )
            {
            // crop the bottom of the image (I can't find a way to centre-crop)
            $image_height = $SCREEN_HEIGHT;
            }
        }
    }
else
    {
    // display image actual size
    }

$x_pos = ($SCREEN_WIDTH - $image_width) / 2;
$y_pos = ($SCREEN_HEIGHT - $image_height) / 2;


echo<<<EOS
<?xml version="1.0" encoding="ISO-8859-1" ?>
<subXML>
    <webPage name="PicasaWebAlbum"
             bgcolor="0x00000000" alpha="$alpha"
             keepAspect="TRUE" fullcolor="TRUE" fullredraw="TRUE"
             >
        <drbList>
            <drb name="Image"
                 x="$x_pos" y="$y_pos"
                 width="$image_width" height="$image_height"
                 hidesurface="TRUE"
                 imagealign="align_center"
                 imagevalign="align_middle"
                 file="$image_url"
                 >
            </drb>
        </drbList>

        <eventList>
            $left_event $right_event
            <event name="OnDraw" command="VFD_TEXT_COMMAND" value="$title"/>
            <event name="OnReturnKey" command="NAVIGATE_PAGE_COMMAND"
                   value="album.php?user_id=$user_id&amp;album_id=$album_id&amp;album_index=$album_index&amp;photo_index=$photo_index"/>
            <event name="OnOkKey" command="NAVIGATE_PAGE_COMMAND"
                   value="album.php?user_id=$user_id&amp;album_id=$album_id&amp;album_index=$album_index&amp;photo_index=$photo_index"/>
            <event name="OnWizTvKey" command="NAVIGATE_PAGE_COMMAND" value="/WizTV/"/>
        </eventList>
    </webPage>
</subXML>
EOS;
